package com.ny.controller;

import com.ny.entity.Result;
import com.ny.entity.User;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin // 添加跨域支持
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public Result selectAllUser(){
        return Result.success(userService.findAllUser());
    }

    // 登录: http://localhost:8081/login
    @PostMapping("/login")
    public Result login(@RequestBody User loginUser){
        List<User> users = userService.login(loginUser.getUsername(), loginUser.getPassword());
        if (users != null && !users.isEmpty()) {
            return Result.success(users.get(0));
        } else {
            return Result.error("用户名或密码错误");
        }
    }
}